package com.admin.education.services.Implements;

import com.admin.education.common.exceptions.ResourceNotFoundException;
import com.admin.education.dataTransferObject.PageRequest;
import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.designations.DesignationsTransfer;
import com.admin.education.dataTransferObject.designations.mapper.DesignationsMapper;
import com.admin.education.dataTransferObject.designations.request.CreateDesignationsRequest;
import com.admin.education.dataTransferObject.designations.request.UpdateDesignationsRequest;
import com.admin.education.models.Designations;
import com.admin.education.models.QDesignations;
import com.admin.education.repositories.DesignationsRepository;
import com.admin.education.services.Interfaces.IDesignationsService;
import com.querydsl.jpa.impl.JPAQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import javax.validation.Valid;
import java.util.List;
@Service
@Transactional
public class DesignationsService implements IDesignationsService {
    @Autowired
    private DesignationsRepository designationsRepository;
    @Autowired
    private DesignationsMapper designationsMapper;
    private QDesignations qDesignations;
    public DesignationsService(){
        qDesignations = QDesignations.designations;
    }

    @Override
    public ServicesExcuteResult<PageResponse<DesignationsTransfer>> queryDesignationss(@Valid PageRequest pageRequest) {
        ServicesExcuteResult<PageResponse<DesignationsTransfer>> servicesExcuteResult =new ServicesExcuteResult<>();

        JPAQuery<Designations> designationsTransferJPAQuery = this.designationsRepository.selectFrom(qDesignations);

        long totalCount = designationsTransferJPAQuery.fetchCount();
        designationsTransferJPAQuery = designationsTransferJPAQuery.orderBy(qDesignations.proportion.desc())
                .offset((pageRequest.getPageIndex()-1)*pageRequest.getPageSize()).limit(pageRequest.getPageSize());

        List<Designations> designations = designationsTransferJPAQuery.fetch();

        List<DesignationsTransfer> designationsTransfers = this.designationsMapper.mapFrom(designations);
        PageResponse<DesignationsTransfer> designationsTransferPageResponse = new PageResponse<>(pageRequest.getPageSize(),pageRequest.getPageIndex(),(int)totalCount,designationsTransfers);

        servicesExcuteResult.setData(designationsTransferPageResponse);
        servicesExcuteResult.setSuccess(true);

        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult createDesignations(@Valid CreateDesignationsRequest createDesignationsRequest) {
        ServicesExcuteResult servicesExcuteResult = new ServicesExcuteResult();

        Designations designations =this.designationsMapper.mapFrom(createDesignationsRequest);

        this.designationsRepository.persist(designations);
        servicesExcuteResult.setSuccess(true);
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult updateDesignations(long designationsId, @Valid UpdateDesignationsRequest updateDesignationsRequest) throws ResourceNotFoundException {
        ServicesExcuteResult servicesExcuteResult = new ServicesExcuteResult();

        JPAQuery<Designations> designationsJPAQuery = this.designationsRepository.selectFrom(qDesignations).where(qDesignations.id.eq(designationsId));
        if(0!=designationsJPAQuery.fetchCount()){
            Designations designations = designationsJPAQuery.fetchOne();

            designations =designationsMapper.mapFrom(updateDesignationsRequest,designations);

            this.designationsRepository.merge(designations);
            servicesExcuteResult.setSuccess(true);
        }
        else {
            throw new ResourceNotFoundException("积分规则不存在");
        }
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult setValid(long designationsId) throws ResourceNotFoundException {
        ServicesExcuteResult servicesExcuteResult= new ServicesExcuteResult();

        JPAQuery<Designations> categorysJPAQuery = this.designationsRepository.selectFrom(qDesignations).where(qDesignations.id.eq(designationsId));
        if(0!=categorysJPAQuery.fetchCount()){
            Designations designations = categorysJPAQuery.fetchOne();

            designations.setValid(!designations.isValid());
            this.designationsRepository.merge(designations);
            servicesExcuteResult.setSuccess(true);
        }
        else {
            throw new ResourceNotFoundException("积分规则不存在");
        }
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult<DesignationsTransfer> queryDesignationsById(long designationsId) throws ResourceNotFoundException {
        ServicesExcuteResult<DesignationsTransfer> servicesExcuteResult= new ServicesExcuteResult();

        JPAQuery<Designations> designationsJPAQuery = this.designationsRepository.selectFrom(qDesignations).where(qDesignations.id.eq(designationsId));
        if(0!=designationsJPAQuery.fetchCount()){
            Designations designations = designationsJPAQuery.fetchOne();

            servicesExcuteResult.setData(this.designationsMapper.mapFrom(designations));
            servicesExcuteResult.setSuccess(true);
        }
        else {
            throw new ResourceNotFoundException("积分规则不存在");
        }
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult deleteDesignations(long designationsId) throws ResourceNotFoundException {
        ServicesExcuteResult servicesExcuteResult= new ServicesExcuteResult();
        JPAQuery<Designations> designationsJPAQuery = this.designationsRepository.selectFrom(qDesignations).where(qDesignations.id.eq(designationsId));

        if(0!=designationsJPAQuery.fetchCount()){
            Designations designations = designationsJPAQuery.fetchOne();

            this.designationsRepository.remove(designations);
            servicesExcuteResult.setSuccess(true);
        }
        else {
            throw new ResourceNotFoundException("积分规则不存在");
        }
        return servicesExcuteResult;
    }
}
